package org.ccay.manage.hypertext.service.impl;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.core.exception.ApplicationException;
import org.ccay.hypertext.HyperText;
import org.ccay.hypertext.MailTemplate;
import org.ccay.hypertext.StaticResource;
import org.ccay.hypertext.dao.IPlainHyperTextDao;
import org.ccay.hypertext.enumerable.HyperCategoryEnum;
import org.ccay.hypertext.service.IHyperTextService;
import org.ccay.hypertext.service.IStaticResourceService;
import org.ccay.manage.hypertext.dao.IHypertextManageDao;
import org.ccay.manage.hypertext.service.IHypertextManageService;

@Named
public class HypertextManageService implements IHypertextManageService{

	@Inject
	@Named("mailTemplateHyperTextService")
	private IHyperTextService<MailTemplate> mailTemplateHyperTextService;
	
	@Inject
	@Named("staticResourceHyperTextService")
	private IStaticResourceService staticResourceHyperTextService;
	
	@Inject
	private IHypertextManageDao hypertextManageDao;
	
	@Inject
	private IPlainHyperTextDao plainHyperTextDao;
	
	@Override
	public PagedResult<? extends HyperText> findPagedHypertextList(HyperText hypertext, PageVO page) {
		if(hypertext.getCategory().equals(Integer.parseInt(HyperCategoryEnum.StaticResource.getValue()))){
			return staticResourceHyperTextService.findPagedHypertextList(createStaticResourceOjb(hypertext), page);
		}else if(hypertext.getCategory().equals(Integer.parseInt(HyperCategoryEnum.MailTemplate.getValue()))){
			return mailTemplateHyperTextService.findPagedHypertextList(createMailTemplateOjb(hypertext), page);
		}else{
			return hypertextManageDao.findPagedHypertextList(hypertext,page);
		}
		
	}

	@Override
	public void create(HyperText hypertext) throws ApplicationException {
		if(hypertext.getId()!=null){
			plainHyperTextDao.delete(hypertext);
		}
		if(hypertext.getCategory().equals(Integer.parseInt(HyperCategoryEnum.StaticResource.getValue()))){
			staticResourceHyperTextService.create(createStaticResourceOjb(hypertext));
		}else if(hypertext.getCategory().equals(Integer.parseInt(HyperCategoryEnum.MailTemplate.getValue()))){
			mailTemplateHyperTextService.create(createMailTemplateOjb(hypertext));
		}else{
			plainHyperTextDao.create(hypertext);
		}
		
	}

	@Override
	public void delete(HyperText hypertext) throws ApplicationException {
		plainHyperTextDao.delete(hypertext);
	}
	
	private StaticResource createStaticResourceOjb(HyperText hypertext){
		StaticResource staticResource = new StaticResource();
		staticResource.setId(hypertext.getId());
		staticResource.setName(hypertext.getName());
		staticResource.setDescription(hypertext.getDescription());
		staticResource.setContent(hypertext.getContent());
		staticResource.setMappingUrl(hypertext.getAddtionProperty().get("mappingUrl"));
		return staticResource;
	}
	
	private MailTemplate createMailTemplateOjb(HyperText hypertext){
		MailTemplate mailTemplate = new MailTemplate();
		mailTemplate.setId(hypertext.getId());
		mailTemplate.setName(hypertext.getName());
		mailTemplate.setDescription(hypertext.getDescription());
		mailTemplate.setContent(hypertext.getContent());
		mailTemplate.setTitle(hypertext.getAddtionProperty().get("title"));
		return mailTemplate;
	}

	@Override
	public HyperText find(HyperText hypertext) throws ApplicationException {
		if(hypertext.getCategory().equals(Integer.parseInt(HyperCategoryEnum.StaticResource.getValue()))){
			return staticResourceHyperTextService.findHyperText(createStaticResourceOjb(hypertext));
		}else if(hypertext.getCategory().equals(Integer.parseInt(HyperCategoryEnum.MailTemplate.getValue()))){
			return mailTemplateHyperTextService.findHyperText(createMailTemplateOjb(hypertext));
		}else{
			return plainHyperTextDao.findHyperText(hypertext);
		}
	}
}
